#include "iokeyboard.h"

void showfd()
{
    system("ls /dev/fd");
}

void remove_stdin(fe_t * fe)
{
    int fd = fe->fd;
    fe_del(fe);
    close(fd);
}

void _stdin_(fe_t* fe)
{
    int fd = fe->fd;
    char line[1024];
    int n;

    log_msg("\e[31mkeyboard input\e[m");

eintr:
    while ((n = read(fd, line, sizeof(line))) > 0) {
        write(1, line, n);
        if (strncmp(line, "quit", 4) == 0) exit(0);
        else if (strncmp(line, "exit", 4) == 0) exit(0);
        else if (strncmp(line, "showfd", 6) == 0) showfd();
        else if (strncmp(line, "closeallfd", 10) == 0) close_all_fd();
    }
    if ( n == 0 ) {
        log_msg("stdin Ctrl+D to close."); remove_stdin(fe); return;
    } else {
        if      (errno == EAGAIN) return;
        else if (errno == EINTR ) goto eintr;
        err_msg("stdin error:%s.", strerror(errno));
    }
}

